package com.ysy.tz.mfg.equipment;

import com.ysy.tz.mfg.equipment.scale.WeightListener;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.ByteBuffer;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Enumeration;

import purejavacomm.CommPortIdentifier;
import purejavacomm.SerialPort;
import purejavacomm.SerialPortEvent;
import purejavacomm.SerialPortEventListener;

public abstract class CommEquipment implements SerialPortEventListener {
    private String portName = "COM1";
    int baudrate;
    private int fractionDigits;
    private String name = "commScale";
    private int timeOut = 1000;
    int dataBits;
    int stopBits;
    int parity;
    private SerialPort serialPort;
    private BigDecimal lastWeight;
    private int sameCount = 5;
    private int currentCount = 0;
    private BigDecimal currentWeight;
    private ArrayList cacheWeights = new ArrayList();
    ByteBuffer buffer = ByteBuffer.allocate(60);
    DecimalFormat decimalFormat = new DecimalFormat("#");
    BigDecimal errorRange;
    private ArrayList<WeightListener> weightListeners = new ArrayList();

    public CommEquipment() {
        this.decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
    }

    public CommEquipment(String portName, int baudrate, int fractionDigits) {
        this.portName = portName;
        this.baudrate = baudrate;
        this.fractionDigits = fractionDigits;
        this.decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
    }

    public void close(){
        this.serialPort.close();
    }

    public void addWeightListener(WeightListener weightListener) {
        this.weightListeners.add(weightListener);
    }

    public void connect() throws Exception {
        Enumeration<CommPortIdentifier> enumeration = CommPortIdentifier.getPortIdentifiers();
        while (enumeration.hasMoreElements()){
            CommPortIdentifier comm = enumeration.nextElement();
            System.out.println("---------------->>> "+comm.getName());
        }
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(this.portName);
        this.serialPort = (SerialPort)portIdentifier.open(this.name, this.timeOut);
        this.serialPort.setSerialPortParams(this.baudrate, this.dataBits, this.stopBits, this.parity);
        this.serialPort.notifyOnDataAvailable(true);
        this.serialPort.addEventListener(this);
    }

    //自己不处理，交给modbus处理
    public void connectNoListener() throws Exception {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(this.portName);
        this.serialPort = (SerialPort)portIdentifier.open(this.name, this.timeOut);
        this.serialPort.setSerialPortParams(this.baudrate, this.dataBits, this.stopBits, this.parity);
        this.serialPort.notifyOnDataAvailable(true);
    }

    protected abstract void messageReceived(InputStream var1) throws Exception;

    public void serialEvent(SerialPortEvent event) {
        if (event.getEventType() == 1) {
            try {
                this.messageReceived(this.serialPort.getInputStream());
            } catch (Exception var3) {
                var3.printStackTrace();
            }
        }

    }

    public void setFractionDigits(int fractionDigits) {
        this.fractionDigits = fractionDigits;
        String pattern = "#";
        if (fractionDigits > 0) {
            pattern = pattern + ".";

            for(int i = 0; i < fractionDigits; ++i) {
                pattern = pattern + "#";
            }
        }

        this.decimalFormat.applyPattern(pattern);
    }

    public void setCurrentWeight(BigDecimal currentWeight) {
        if (this.sameCount <= 1) {
            this.setRealWeight();
        } else {
            if (this.currentWeight == null) {
                this.currentWeight = currentWeight;
                this.currentCount = 1;
            } else if (this.currentWeight.compareTo(currentWeight) == 0) {
                ++this.currentCount;
                if (this.currentCount == this.sameCount) {
                    this.setRealWeight();
                } else if (this.currentCount > this.sameCount) {
                }
            } else {
                this.currentWeight = currentWeight;
                this.currentCount = 1;
            }

        }
    }

    private void setRealWeight() {
        if (this.lastWeight != null && null != this.errorRange) {
            BigDecimal inaccurate = this.currentWeight.subtract(this.lastWeight).abs();
            if (inaccurate.compareTo(this.errorRange) < 1) {
                return;
            }
        }

        this.lastWeight = this.currentWeight;
        System.out.println("--------->>> 取到值 " + this.currentWeight);
        this.weightListeners.forEach((weightListener) -> {
            weightListener.obtainWeight(this.currentWeight.toString());
        });
    }

    public static void main(String[] args) {
        CommEquipment commEquipment = new CommEquipment() {
            protected void messageReceived(InputStream inputStream) throws Exception {
            }
        };
        commEquipment.setCurrentWeight(new BigDecimal("99.5"));
        commEquipment.setCurrentWeight(new BigDecimal("99.5"));
        commEquipment.setCurrentWeight(new BigDecimal("99.5"));
        commEquipment.setCurrentWeight(new BigDecimal("99.5"));
        commEquipment.setCurrentWeight(new BigDecimal("99.5"));
        commEquipment.setCurrentWeight(new BigDecimal("99.4"));
        commEquipment.setCurrentWeight(new BigDecimal("99.4"));
        commEquipment.setCurrentWeight(new BigDecimal("99.4"));
        commEquipment.setCurrentWeight(new BigDecimal("99.4"));
        commEquipment.setCurrentWeight(new BigDecimal("99.4"));
        commEquipment.setCurrentWeight(new BigDecimal("99.6"));
        commEquipment.setCurrentWeight(new BigDecimal("99.6"));
        commEquipment.setCurrentWeight(new BigDecimal("99.6"));
        commEquipment.setCurrentWeight(new BigDecimal("99.6"));
        commEquipment.setCurrentWeight(new BigDecimal("99.6"));
        commEquipment.setCurrentWeight(new BigDecimal("99.7"));
        commEquipment.setCurrentWeight(new BigDecimal("99.7"));
        commEquipment.setCurrentWeight(new BigDecimal("99.7"));
        commEquipment.setCurrentWeight(new BigDecimal("99.7"));
        commEquipment.setCurrentWeight(new BigDecimal("99.7"));
    }

    public int getSameCount() {
        return this.sameCount;
    }

    public void setSameCount(int sameCount) {
        this.sameCount = sameCount;
    }

    public String getPortName() {
        return this.portName;
    }

    public int getBaudrate() {
        return this.baudrate;
    }

    public int getFractionDigits() {
        return this.fractionDigits;
    }

    public String getName() {
        return this.name;
    }

    public int getTimeOut() {
        return this.timeOut;
    }

    public int getDataBits() {
        return this.dataBits;
    }

    public int getStopBits() {
        return this.stopBits;
    }

    public int getParity() {
        return this.parity;
    }

    public SerialPort getSerialPort() {
        return this.serialPort;
    }

    public BigDecimal getLastWeight() {
        return this.lastWeight;
    }

    public int getCurrentCount() {
        return this.currentCount;
    }

    public BigDecimal getCurrentWeight() {
        return this.currentWeight;
    }

    public ArrayList getCacheWeights() {
        return this.cacheWeights;
    }

    public ByteBuffer getBuffer() {
        return this.buffer;
    }

    public DecimalFormat getDecimalFormat() {
        return this.decimalFormat;
    }

    public BigDecimal getErrorRange() {
        return this.errorRange;
    }

    public ArrayList<WeightListener> getWeightListeners() {
        return this.weightListeners;
    }

    public void setPortName(String portName) {
        this.portName = portName;
    }

    public void setBaudrate(int baudrate) {
        this.baudrate = baudrate;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setTimeOut(int timeOut) {
        this.timeOut = timeOut;
    }

    public void setDataBits(int dataBits) {
        this.dataBits = dataBits;
    }

    public void setStopBits(int stopBits) {
        this.stopBits = stopBits;
    }

    public void setParity(int parity) {
        this.parity = parity;
    }

    public void setSerialPort(SerialPort serialPort) {
        this.serialPort = serialPort;
    }

    public void setLastWeight(BigDecimal lastWeight) {
        this.lastWeight = lastWeight;
    }

    public void setCurrentCount(int currentCount) {
        this.currentCount = currentCount;
    }

    public void setCacheWeights(ArrayList cacheWeights) {
        this.cacheWeights = cacheWeights;
    }

    public void setBuffer(ByteBuffer buffer) {
        this.buffer = buffer;
    }

    public void setDecimalFormat(DecimalFormat decimalFormat) {
        this.decimalFormat = decimalFormat;
    }

    public void setErrorRange(BigDecimal errorRange) {
        this.errorRange = errorRange;
    }

    public void setWeightListeners(ArrayList<WeightListener> weightListeners) {
        this.weightListeners = weightListeners;
    }
}
